package smcms.service.cloudstore.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import smcms.commons.api.entity.result.CommonsResult;
import smcms.service.cloudstore.entity.*;
import smcms.service.cloudstore.feignservices.SsoFeignService;
import smcms.service.cloudstore.mapper.StoreManagerMapper;
import smcms.service.cloudstore.service.StoreManagerService;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@Transactional
public class StoreManagerServiceImpl implements StoreManagerService {
    @Autowired
    private StoreManagerMapper storeManagerMapper;
    @Autowired
    private CommonsResult commonsResult;
    @Autowired
    private SsoFeignService ssoFeignService;
    @Override
    @Deprecated
    public CommonsResult addCloudStore(CloudStoreReq req) {
        if (ObjectUtils.isEmpty(req.getCloudStoreShopkeeperAccount())) {
            req.setCloudStoreShopkeeperAccount(req.getStoreCreatorAccount());
            StaffInfoResp staffInfoResp = storeManagerMapper.queryUser(req.getStoreCreatorAccount(),req.getCompanyId());
            if (ObjectUtils.isEmpty(staffInfoResp)) {
                return commonsResult.failed("查询不到账号信息！");
            }
            CloudStoreReq userInfo = new CloudStoreReq();
            userInfo.setCloudStoreShopkeeperAccount(req.getStoreCreatorAccount());
            userInfo.setCloudStoreName("所属小店");
            storeManagerMapper.updateCloudShopKeeper(req);
            req.setCloudStoreShopkeeperName(staffInfoResp.getStaffName());
            req.setCloudStoreId(UUID.randomUUID().toString());
            req.setStoreOwnerAccount(staffInfoResp.getStoreOwnerAccount());
            storeManagerMapper.addCloudStore(req);
            return commonsResult.success("新增小店成功");
        }
        StaffInfoResp staffInfoResp = storeManagerMapper.queryUser(req.getCloudStoreShopkeeperAccount(),req.getCompanyId());
        storeManagerMapper.updateCloudShopKeeper(req);
        if (ObjectUtils.isEmpty(staffInfoResp)) {
            return commonsResult.failed("查询不到账号信息！");
        }
        req.setCloudStoreId(UUID.randomUUID().toString());
        storeManagerMapper.addCloudStore(req);
        return commonsResult.success("新增小店成功");
    }
    @Override
    public CommonsResult addCloudStoreInfo(CloudStoreReq req) {
        if (ObjectUtils.isEmpty(req.getCompanyId())) {
            return commonsResult.failed("云id不能为空！");
        }
        StaffInfoResp staffInfoResp = storeManagerMapper.queryUser(req.getCloudStoreShopkeeperAccount(),req.getCompanyId());
        storeManagerMapper.updateCloudShopKeeper(req);
        if (ObjectUtils.isEmpty(staffInfoResp)) {
            return commonsResult.failed("查询不到账号信息！");
        }
        req.setCloudStoreShopkeeperName(staffInfoResp.getStaffName());
        req.setCloudStoreId(UUID.randomUUID().toString());
        req.setStoreOwnerAccount(staffInfoResp.getStoreOwnerAccount());
        String[] cateList = req.getCloudStoreType().split(",");
        List<ProductCategoryReq> categoryReqList = Arrays.stream(cateList).map(e -> {
            ProductCategoryReq productCategoryReq = new ProductCategoryReq();
            productCategoryReq.setCategoryName(e);
            productCategoryReq.setCloudId(req.getCompanyId());
            productCategoryReq.setCloudStoreId(req.getCloudStoreId());
            productCategoryReq.setCloudStoreName(req.getCloudStoreName());
            return productCategoryReq;
        }).collect(Collectors.toList());
        storeManagerMapper.addProductType(categoryReqList);
        storeManagerMapper.addCloudStore(req);
        return commonsResult.success("新增小店成功");
    }

    @Override
    public CommonsResult delCloudStore(CloudStoreReq req) {
        if(StringUtils.isEmpty(req.getCloudStoreId())) {
            return commonsResult.failed("小店ID不能为空！");
        }
        storeManagerMapper.delCloudStore(req.getCloudStoreId());
        return commonsResult.success("云小店被成功删除");
    }

    @Override
    @Deprecated
    public List<CloudStoreResp> queryCloudStore(CloudStoreReq req) {
        if (ObjectUtils.isEmpty(req.getCloudStoreShopkeeperAccount())) {
            throw new IllegalArgumentException("登录用户不能为空");
        }
        StaffInfoReq staffInfoReq = new StaffInfoReq();
        staffInfoReq.setUsername(req.getCloudStoreShopkeeperAccount());
        staffInfoReq.setRoleName("shopkeeper");
        CommonsResult commonsResult = ssoFeignService.getStaffList(staffInfoReq);
        List<StaffInfoResp> staffInfoRespList = (List<StaffInfoResp>) commonsResult.getData();
        StaffInfoResp staffInfoResp = new StaffInfoResp();
        staffInfoResp.setAccount(req.getCloudStoreShopkeeperAccount());
        staffInfoRespList.add(staffInfoResp);
        List<CloudStoreResp> cloudStoreRespList = storeManagerMapper.queryCloudStore(req,staffInfoRespList);
        return cloudStoreRespList;
    }

    public List<CloudStoreResp> queryCloudStoreList(CloudStoreReq req) {
        if (ObjectUtils.isEmpty(req.getCloudStoreShopkeeperAccount())) {
            throw new IllegalArgumentException("登录用户不能为空");
        }
        List<CloudStoreResp> cloudStoreRespList = storeManagerMapper.queryCloudStoreList(req.getCloudStoreShopkeeperAccount());
        return cloudStoreRespList;
    }
}
